<?php
/**
 * 菜单Control类
 * @file 	MenuControl.class.php
 * @author 	kelecode@gmail.com
 * @date 	2013-06-13
 * modify       
 */
class MenuControl extends Control{

    public function __construct(){
		parent::__construct();
		
		$mainSubMenu = APP_ROOT . '/template/' . C('theme') . '/menu/main_sub_menu.php';
		$css = APP_ROOT . '/template/' . C('theme') . '/common/css.php';
		$script =  APP_ROOT . '/template/' . C('theme') . '/common/js.php';
		$this->assign('css',$css);
		$this->assign('script',$script);
		$this->assign('mainSubMenu',$mainSubMenu);
	}

	public function index(){
		$this->lists();
	}
	
	/**
	 * 菜单列表
	 */
	public function lists(){
		$lists = M('Menu')->menuList();
		$main = APP_ROOT . '/template/' . C('theme') . '/menu/lists.php';
		
		$this->assign('main',$main);
		$this->assign('lists',$lists);
		$this->display('layout','common');
	}
	
	/**
	 * 新加菜单
	 */
	public function add($type='add'){
		$parent_menu = array(
			array('id' => 0, 'title' => '根菜单'),
			array('id' => 1, 'title' => '首页'),
		);
		$menu_type = array(
			array('id' => 1, 'name' => '首页'),
			array('id' => 2, 'name' => '分类'),
			array('id' => 3, 'name' => '组件'),
			array('id' => 4, 'name' => '外链'),
		);
		
		$component = array(
			array('id' => 1, 'name' => 'LinkCloud'),
			array('id' => 2, 'name' => 'Blog'),
		);
		
		$sort = array_keys($parent_menu);
		$sort = end($sort) + 1;
		
		$this->assign('parent_menu',$parent_menu);
		$this->assign('menu_type',$menu_type);
		$this->assign('component',$component);
		$this->assign('sort',$sort);
		
		if($_POST['dosubmit'] == 1){
			$data = array(
				'id' => (int)$_POST['id'],
				'menu_type' => $_POST['menu_type'],
				'name' => $_POST['name'],
				'alias' => $_POST['alias'],
				'link' => $_POST['link'],
				'link_type' => $_POST['link_type'],
				'parent_id' => (int)$_POST['parent_id'],
				'component_id' => (int)$_POST['component_id'],
				'sort' => (int)$_POST['sort'],
				'params' => $_POST['params'],
				'status' => (int)$_POST['status'],
			);
			
			if(empty($data['id'])){
				$result = M('Menu')->addMenu($data);
			}else{
				$result = M('Menu')->editMenu($data);
			}
			
			$url = C('backend').'index.php?c=menu&m=menuList';
			$msg = '操作'.($result ? '成功' : '失败').'...';
			redirect($url,2,$msg);
		}else{
			//展示表单
			$id = (int)$_GET['id'];
			if( !empty($id) ){
				$menu = M('Menu')->getMenuById($id);
			}
		}
		$main = APP_ROOT . '/template/' . C('theme') . '/menu/add.php';
		
		$this->assign('main',$main);
		$this->assign('id',$id);
		$this->assign('menu',$menu);
		$this->assign('token',token());
		$this->display('layout','common');
	}
	
	/**
	 * 编辑菜单
	 */
	public function edit(){
		$this->add('edit');
	}
	
	/**
	 * 删除菜单
	 * @return json
	 */
	public function del(){
		$id = (int)$_POST['id'];
		$result = M('Category')->delMenu($id);
		$info = '删除'.($result ?  '成功' : '失败');
		$status = (int)$result;
		ajaxReturn('',$info,$status);
	}	
	
	/**
	 * 更新状态
	 */
	public function updateStatus(){
		$ids = getgpc('ids');
		empty($ids) && ajaxReturn('','请先选择要操作的菜单',0);
		$ids = explode(',',$ids);
		$status = (int)getgpc('status');
		$result = M('Menu')->updateStatus($ids, $status);
		$info = '操作'.($result ?  '成功' : '失败');
		$status = (int)$result;
		ajaxReturn('',$info,$status);
	}
	
	/**
	 * 更新菜单排序
	 */
	public function updateSort(){
		$id = getgpc('id');
		empty($id) && ajaxReturn('','请先选择要操作的菜单',0);
		$num = (int)getgpc('num');
		$num = $num <= 0 ? 0 : $num;
		$result = M('Menu')->updateSort($id, $num);
		$info = '操作'.($result ?  '成功' : '失败');
		$status = (int)$result;
		ajaxReturn('',$info,$status);
	}

}